//
//  ZCEssenceTableViewController.m
//  百思不得姐
//
//  Created by Ning Xie on 16/8/29.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCEssenceTableViewController.h"
#import "ZCEssenceModel.h"
#import "ZCEssenceCell.h"
#import "ZCCommentViewController.h"
#import "ZCNewViewController.h"


#define EssenceTableViewCellIdentifier @"EssenceTableViewCellIdentifier"



@interface ZCEssenceTableViewController ()

@property (strong, nonatomic) NSMutableArray *essenceArray;

@property (copy, nonatomic) NSString *maxid;

@property (assign, nonatomic) NSInteger page;



@end

@implementation ZCEssenceTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setupUI];
    [self.tableView.mj_header beginRefreshing];
  
}
//MARK:懒加载数组
-(NSMutableArray *)essenceArray{
    if (_essenceArray == nil) {
        _essenceArray = [NSMutableArray array];
    }
    return _essenceArray;
}

-(void)setupUI{
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = ZCGlobalRBG;
    self.tableView.estimatedRowHeight = 300;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    [self.tableView registerClass:[ZCEssenceCell class] forCellReuseIdentifier:EssenceTableViewCellIdentifier];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(contentViewLoadMoreData)];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(contentViewLoadNewData)];
    self.tableView.mj_footer.hidden = YES;
    

}


//MARK:上拉加载
-(void)contentViewLoadMoreData{

    
    [self.tableView.mj_header endRefreshing];
    [self requestDataWithType:self.dataType maxid:self.maxid page:self.page];
}

//MARK:下拉刷新
-(void)contentViewLoadNewData{
    
    
    [self.tableView.mj_footer endRefreshing];
    [self requestDataWithType:self.dataType maxid:nil page:0];
}


//MARK:加载数据
-(void)loadData{
    
    self.page = 0;
    if (self.essenceArray.count > 0) {
        [self.tableView reloadData];
        return;
    }
    [self requestDataWithType:self.dataType maxid:nil page:self.page];
    
    
}
-(NSString*)a{
    
    return [self.parentViewController isKindOfClass:[ZCNewViewController class]]? @"newlist":@"list";
}

//MARK:请求网络数据
-(void)requestDataWithType:(DataType)type maxid:(NSString*)maxid page:(NSInteger)page{
    
    
//    [SVProgressHUD show];
//    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [[ZCNetworkingTools sharedNetWorkingTools] requestEssenceWithMaxid:maxid page:page type:type a:self.a callBack:^(NSDictionary *response, NSError *error) {
        
        if (error != nil) {
            ZCLog(@"数据请求错误 %@",error);
            
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
            [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
            return ;
        }
        if (![response isKindOfClass:[NSDictionary class]]) {
            ZCLog(@"不是一个合法的字典");
            
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
            [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
            return;
        }
//        [SVProgressHUD dismiss];
        
//        ZCLog(@"%@",response);
//        ZCLog(@"%@",response[@"list"]);
        self.maxid = response[@"info"][@"maxid"];

        
        if (maxid == nil) {
            self.page = 0;
            self.essenceArray = [ZCEssenceModel mj_objectArrayWithKeyValuesArray:response[@"list"]];
            
        }else{
            self.page++;
            NSArray *listArray = [ZCEssenceModel mj_objectArrayWithKeyValuesArray:response[@"list"]];
            [self.essenceArray addObjectsFromArray:listArray];
        }
        
        
//        ZCLog(@"%ld",self.essenceArray.count);
        [self.tableView reloadData];
        
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
    }];
}

//MARK: TableView数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSInteger count = self.essenceArray.count;
    self.tableView.mj_footer.hidden = (count == 0);
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZCEssenceCell *cell = [tableView dequeueReusableCellWithIdentifier:EssenceTableViewCellIdentifier forIndexPath:indexPath];
    cell.indexPath = indexPath;
    
    ZCEssenceModel *model = self.essenceArray[indexPath.row];
    cell.model = model;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZCCommentViewController *comment = [[ZCCommentViewController alloc]init];
    
    ZCEssenceModel *model = self.essenceArray[indexPath.row];

    comment.model = model;
    
    [self.navigationController pushViewController:comment animated:YES];
}

@end
